# BASE IMAGE
FROM alpine:latest

# MAINTAINER
MAINTAINER JiaXiaohei <i@jiaxiaohei.com>

ENV TENGINE_VERSION 2.3.3
ENV GROUP_ID 1000
ENV USER_ID 1000

ENV TENGINE_HOME "/app/tengine/tengine-$TENGINE_VERSION"
ENV PATH $TENGINE_HOME/sbin:$PATH

ENV CONFIG "\
--user=tengine \
--prefix=/app/tengine/tengine-$TENGINE_VERSION \
--conf-path=/app/conf/nginx.conf \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_realip_module \
"

WORKDIR /app

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
    && addgroup -g $GROUP_ID -S tengine \
    && adduser tengine -u $USER_ID -h /home/tengine -D -S -s /bin/bash -G tengine \
    && apk add --no-cache --virtual .build \
    gcc \
    zlib \
    wget \
    libc-dev \
    make \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    curl \
    libxslt-dev \
    gd-dev \
    geoip-dev \
    && mkdir -p /app/tengine \
    && mkdir -p /app/web/ip \
    && mkdir -p /app/conf/vhost \
    && cd /home/tengine/ \
    && wget "http://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz" -O tengine-$TENGINE_VERSION.tar.gz \
    && tar -zxvf tengine-$TENGINE_VERSION.tar.gz \
    && rm tengine-$TENGINE_VERSION.tar.gz \
    && cd /home/tengine/tengine-$TENGINE_VERSION \
    && ./configure $CONFIG \
    && make \
    && make install \
    && rm -rf /home/tengine/tengine-$TENGINE_VERSION \
    && mv -f /app/tengine/tengine-$TENGINE_VERSION/html /app/web/ip/html \
    && chown -R $GROUP_ID:$USER_ID /app/ \
    && sed -i 's/listen       80;/listen       52180;/g' /app/conf/nginx.conf \
    && sed -i 's#root   html;#root   /app/web/ip/html;#g' /app/conf/nginx.conf \
    && sed -i '$s#}#include vhost/*.conf; }#g' /app/conf/nginx.conf \
    && PATH="/app/tengine/tengine-$TENGINE_VERSION/sbin:$PATH" 

EXPOSE 52180 52143
USER $GROUP_ID:$USER_ID
CMD ["nginx", "-g", "daemon off;"] 